<!--
 * createor: zc
 * email: zc8023zs@163.com
 * create_time: 2020/3/14 13:46:24
 *-->
<template>
  <el-container>
    <el-header style="margin:20px 10px 0 10px;height:auto">
      <el-form :inline="true" :model="listQuery">
        <el-form-item label="运营中心id:">
          <el-input v-model="listQuery.oc_id" clearable="clearable" maxlength="99999999999"></el-input>
        </el-form-item> 
        <el-form-item label="店铺/门店id:">
          <el-input v-model="listQuery.shop_id" clearable="clearable" maxlength="99999999999"></el-input>
        </el-form-item> 
        <el-form-item label="商品分类:">
          <el-input v-model="listQuery.category_id" clearable="clearable" maxlength="99999999999"></el-input>
        </el-form-item> 
        <el-form-item label="商品名称:">
          <el-input v-model="listQuery.name" clearable="clearable" maxlength="255"></el-input>
        </el-form-item> 
        <el-form-item>
          <el-dropdown split-button type="primary" @click="onSearch" @command="onSearch" :loading="tabLoading">
            <i class="el-icon-search el-icon--left"></i>{{$t('table.search')}}
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command='all'>查询全部</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-form-item> 
        <el-form-item>
          <el-button type="primary" icon="el-icon-edit" @click="onAdd">{{$t('table.add')}}</el-button>
        </el-form-item> 
      </el-form>
    </el-header>
    <el-main>
      <el-table
        :key="tableKey"
        :data="listData" :height="tableHeight"
        highlight-current-row border fit
        v-loading="tabLoading"
      >
        <el-table-column type="index" label=" " width="50" align="center" :index="table_index" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="goods_id" label="商品id" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="oc_id" label="运营中心id" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="shop_id" label="店铺/门店id" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="category_id" label="商品分类" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="name" label="商品名称" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="logo" label="店铺logo" width="120" align="center" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-popover trigger="click" placement="top">
              <img :src="scope.row.logo" height="300" />
              <div slot="reference" class="name-wrapper">
                <img :src="scope.row.logo" alt="img" style="height:30px" />
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="min_buy" label="最小购买量" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="unit" label="单位" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="intro" label="商品简介" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="description" label="商品详情" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="tags" label="商品标签" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="create_time" label="创建时间" width="120" align="center" :formatter="dateFormat" :show-overflow-tooltip="true"/>
        <el-table-column prop="unsend" label="单点不配送" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="status" label="状态" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="rpt_like" label="点赞数" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="rpt_favorite" label="收藏数" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="rpt_comment" label="评论数" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column
          :label="$t('table.actions')"
          align="left"
          min-width="230"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-dropdown @command="onInfoPages">
              <span class="el-dropdown-link">
                <i class="el-icon-s-tools el-icon--left"></i>功能操作<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item icon="el-icon-edit" :command="{a:scope,b:'edit'}">{{$t('table.edit')}}</el-dropdown-item> 
                <el-dropdown-item icon="el-icon-search" :command="{a:scope,b:'info'}">{{$t('table.info')}}</el-dropdown-item> 
                <el-dropdown-item icon="el-icon-delete" :command="{a:scope,b:'delete'}">{{$t('table.delete')}}</el-dropdown-item> 
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="pagecnt>1"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.size"
        @pagination="getList"
      />
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :model="formData" label-width="200px" style="padding:5% 5% 0 5%">
          <input type="hidden" v-model="formData.goods_id"/>
        <el-form-item label="运营中心id：">
          <el-input v-model="formData.oc_id" placeholder="请输入运营中心id"></el-input>
        </el-form-item>
        <el-form-item label="店铺/门店id：">
          <el-input v-model="formData.shop_id" placeholder="请输入店铺/门店id"></el-input>
        </el-form-item>
        <el-form-item label="商品分类：">
          <el-input v-model="formData.category_id" placeholder="请输入商品分类"></el-input>
        </el-form-item>
        <el-form-item label="商品名称：">
          <el-input v-model="formData.name" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item label="店铺logo：">
          <div class="avatar-uploader">
            <div class="el-upload el-upload--text">
              <el-upload
                class="avatar-uploader"
                accept="image/png, image/jpeg"
                action = ""
                :show-file-list="false"
                :before-upload="logoBeforeAvatarUpload"> 
                <img v-if="formData.logo" :src="uploadrooturl+formData.logo" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="最小购买量：">
          <el-input v-model="formData.min_buy" placeholder="请输入最小购买量"></el-input>
        </el-form-item>
        <el-form-item label="单位：">
          <el-input v-model="formData.unit" placeholder="请输入单位"></el-input>
        </el-form-item>
        <el-form-item label="商品简介：">
          <el-input v-model="formData.intro" placeholder="请输入商品简介"></el-input>
        </el-form-item>
        <el-form-item label="商品详情：">
          <el-input v-model="formData.description" placeholder="请输入商品详情"></el-input>
        </el-form-item>
        <el-form-item label="商品标签：">
          <el-input v-model="formData.tags" placeholder="请输入商品标签"></el-input>
        </el-form-item>
        <el-form-item label="创建时间：">
          <el-input v-model="formData.create_time" placeholder="请输入创建时间"></el-input>
        </el-form-item>
        <el-form-item label="单点不配送：">
          <el-input v-model="formData.unsend" placeholder="请输入单点不配送"></el-input>
        </el-form-item>
        <el-form-item label="状态：">
          <el-input v-model="formData.status" placeholder="请输入状态"></el-input>
        </el-form-item>
        <el-form-item label="点赞数：">
          <el-input v-model="formData.rpt_like" placeholder="请输入点赞数"></el-input>
        </el-form-item>
        <el-form-item label="收藏数：">
          <el-input v-model="formData.rpt_favorite" placeholder="请输入收藏数"></el-input>
        </el-form-item>
        <el-form-item label="评论数：">
          <el-input v-model="formData.rpt_comment" placeholder="请输入评论数"></el-input>
        </el-form-item>
        <el-form-item label="数据版本字段：">
          <el-input v-model="formData.version_id" placeholder="请输入数据版本字段"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
          <el-button
            type="primary"
            v-if="curdAction=='update' || curdAction=='add'"
            @click="onSave"
            :loading="saveLoading" v-text="$t('table.save')"
          ></el-button> 
        <el-button @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    </el-main>
  </el-container>
</template>
<style scoped>
body .app-container .el-container,
body .app-container .el-header,
body .app-container .el-main,
body .el-container,
body .el-header,
body .el-main {
  padding: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
</style>
<script>
import Pagination from "@/components/Pagination";
import { Loading } from "element-ui";
import {
  qr_goodsSelecttree,
  qr_goodsSelectpage,
  qr_goodsSelect,
  qr_goodsAdd,
  qr_goodsUpdate,
  qr_goodsDel,
  qr_goodsInfo
} from '@/api/project/wmyw/project_wmyw_qr_goods'
export default {
  name: "a_wmywa_qr_goods",
  components: { Pagination, },
  filters: {},
  data() {
    return {
      dialogTitle: `门店商品编辑`,
      curdAction: "",
      saveLoading: false,
      delLoading: false,
      tabLoading: false,
      dialogFormVisible: false,
      tableKey: 'wmyw_qr_goods_table',
      listData: null,
      total: 0,
      pagecnt: 0,
      tableHeight: (window.innerHeight-210),
      listQuery: {
        page: 1,
        size: 20,
        oc_id: null, 
        shop_id: null, 
        category_id: null, 
        name: null, 
      },
      formData: {
        goods_id: null, 
        oc_id: null, 
        shop_id: null, 
        category_id: null, 
        name: null, 
        logo: null, 
        min_buy: null, 
        unit: null, 
        intro: null, 
        description: null, 
        tags: null, 
        create_time: null, 
        unsend: null, 
        status: null, 
        rpt_like: null, 
        rpt_favorite: null, 
        rpt_comment: null, 
        version_id: null, 
      },
    };
  },
  mounted(){
    let that =this;
    window.onresize=()=>{
      this.tableHeight = (window.innerHeight-210);
    }
  },
  created() {
    this.getList();
  },
  methods: {
    logoHandleAvatarSuccess(r, file){
      this.formData.logo = r.dataSingle.videopath;
    },
    logoBeforeAvatarUpload(file){
      const formData = new FormData();
      formData.append('file', file);
      fileUp(formData,function(progressEvent){
      }).then(r => {
         this.logoHandleAvatarSuccess(r, file);
      }).catch(err => {
        console.log(err);
      });
      return false;
    },
    onSearch(command){
      if(command && command=='all'){
        this.resetQuery();
      }
      this.listQuery.page=1;
      this.getList();
    },
    onInfoPage(scope){
      var view = this.$store.state.permission.routesMap["/wmyw/qr_goods_info"];
      this.$router.push({ path: view.path, query: {m_id:view.code_aes,id:scope.row.goods_id} })
    },
    onInfoPages(commend){
      if(commend.b=='edit')this.onEdit(commend.a);
      if(commend.b=='info')this.onInfoPage(commend.a);
      if(commend.b=='delete')this.onDel(commend.a);
    },
    resetQuery(){
      this.listQuery = {
        page: 1,
        size: 20,
        oc_id: null, 
        shop_id: null, 
        category_id: null, 
        name: null, 
      }
    },
    reset(){
      this.formData = {
        goods_id: null, 
        oc_id: null, 
        shop_id: null, 
        category_id: null, 
        name: null, 
        logo: null, 
        min_buy: null, 
        unit: null, 
        intro: null, 
        description: null, 
        tags: null, 
        create_time: null, 
        unsend: null, 
        status: null, 
        rpt_like: null, 
        rpt_favorite: null, 
        rpt_comment: null, 
        version_id: null, 
      };
    },
    onAdd(){
      this.dialogFormVisible=true;
      this.curdAction = "add";
      this.dialogTitle=`门店商品添加`;
      this.reset();
    },
    onEdit(data){
      this.onInfo(data);
      this.dialogFormVisible=true;
      this.curdAction = "update";
      this.dialogTitle=`门店商品编辑`;
      this.reset();
    },
    onSave(){
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.saveLoading = true;
          if (this.curdAction == "add") {
            qr_goodsAdd(this.$qs.stringify(this.formData)).then(r => {
                this.getList();
                this.reset();
                this.curdAction = '';
                this.saveLoading  = false;
                this.dialogFormVisible = false
            }).catch(()=>{
              this.saveLoading=false;
            });
          } else if (this.curdAction == "update") {
            qr_goodsUpdate(this.$qs.stringify(this.formData)).then(r => {
                this.getList();
                this.reset();
                this.curdAction = '';
                this.saveLoading  = false;
                this.dialogFormVisible = false
            }).catch(()=>{
              this.saveLoading=false;
            });
          }
        }
      });
    },
    onDel(data){
        var that = this;
        this.$confirm('确定删除?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            qr_goodsDel(that.$qs.stringify({id:data.row.goods_id})).then(r=>{
                that.getList();
                that.reset();
                that.curdAction = '';
                that.delLoading  = false;
                that.$notify({
                    message:"删除成功",
                    type: "success"
                });
            }); 
        }).catch(() => {
            this.$notify({
                message: "已取消删除",
                type: "info"
            }); 
        });
    },
    onInfo(data){
        qr_goodsInfo({id:data.row.goods_id}).then(r=>{
            this.formData.goods_id=r.dataSingle.goods_id;
            this.formData.oc_id=r.dataSingle.oc_id;
            this.formData.shop_id=r.dataSingle.shop_id;
            this.formData.category_id=r.dataSingle.category_id;
            this.formData.name=r.dataSingle.name;
            this.formData.logo=r.dataSingle.logo;
            this.formData.min_buy=r.dataSingle.min_buy;
            this.formData.unit=r.dataSingle.unit;
            this.formData.intro=r.dataSingle.intro;
            this.formData.description=r.dataSingle.description;
            this.formData.tags=r.dataSingle.tags;
            this.formData.create_time=r.dataSingle.create_time;
            this.formData.unsend=r.dataSingle.unsend;
            this.formData.status=r.dataSingle.status;
            this.formData.rpt_like=r.dataSingle.rpt_like;
            this.formData.rpt_favorite=r.dataSingle.rpt_favorite;
            this.formData.rpt_comment=r.dataSingle.rpt_comment;
            this.formData.version_id=r.dataSingle.version_id;
        });
    },
    dateFormat(row, column, cellValue, index){
      return this.$moment(cellValue).format("YYYY/MM/DD");
    }, 
    commentFormat(row, column, cellValue, index){
      try {
        var comment = "";
        var array = comment.split(':')[1].split(' ');
        var obj = {};
        for(var i=0;i<array.length;i++){
          var split = array[i].split('=');
          obj[split[1]]=split[0];
        }
        if(obj[cellValue]) return obj[cellValue];
        return cellValue;
      } catch (error) {
        return cellValue;
      }
    },
    table_index(index) {
      return (this.listQuery.page - 1) * this.listQuery.size + index + 1;
    },
    getList() {
      this.tabLoading=true;
      qr_goodsSelectpage(this.$qs.stringify(this.listQuery,{ indices: false })).then(r=>{
        this.listData = r.dataArray.dataList;
        this.total = r.dataArray.dataCount;
        this.pagecnt = r.dataArray.pageCount;
        this.tabLoading=false;
      }).catch(()=>{
        this.tabLoading=false;
      });
    }
  }
};
</script>